package com.ldl.origami.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

/**
 * 功能描述:
 * 作者: LDL
 * 创建时间: 2014/8/26 14:56
 */
public class OrigamiUtils {

    /**
     * 根据积分获取等级名称
     * @param integral
     * @param levelMap
     * @return
     */
    public static String getLevelNameByIntegral(int integral,HashMap<String,String> levelMap){
        String leveName = "";

        Iterator iterator = levelMap.entrySet().iterator();

        while(iterator.hasNext()){
            Map.Entry entry = (Map.Entry) iterator.next();
            String integralRange = (String) entry.getValue();
            String[] integrals = integralRange.split(",");
            int min = Integer.parseInt(integrals[0]);
            int max = Integer.parseInt(integrals[1]);
            if(Math.max(integral,min)==Math.min(max,integral)){
                leveName = (String) entry.getKey();
            }
        }
        return leveName;
    }


    /**
     * Exception 转换成字符串
     * @param e
     * @return
     */
    public static String getErrorInfoFromException(Exception e){
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        return sw.getBuffer().toString();
    }


    /**
     * 获得一个UUID
     *
     * @return String UUID
     */
    public static String getUUID() {
        String s = UUID.randomUUID().toString();
        //去掉“-”符号
        return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
    }

}
